package com.excilys.capicsoubank.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.GrantedAuthority;

/**
 * POJO Role.
 * 
 * @author Tiphaine Ruy
 * 
 */

@Entity
@Table(name = "ROLE")
public class Role implements Serializable, GrantedAuthority {

	/**
	 * Les roles possibles.
	 * 
	 * @author "Grégory Coutant"
	 * 
	 */
	public enum AuthorityType {
		ROLE_USER, ROLE_ADMIN;
	}

	private static final long serialVersionUID = 2035603094981791673L;

	private Integer roleId;

	private AuthorityType authority;

	@Override
	@Enumerated(EnumType.STRING)
	@Column(name = "role")
	public String getAuthority() {
		return authority.name();
	}

	public void setAuthority(String role) {
		this.authority = AuthorityType.valueOf(role);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "role_id")
	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	@Override
	public int hashCode() {
		return roleId;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (obj == null)
			return false;

		if (getClass() != obj.getClass())
			return false;

		Role role = (Role) obj;

		return ObjectUtils.equals(roleId, role.getRoleId());
	}
}
